package com.example.wechat.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.wechat.model.Friend;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class FriendDatabaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "friend.db";
    private static final int DATABASE_VERSION = 1;

    // 表名
    public static final String TABLE_FRIENDS = "friends";
    public static final String TABLE_GROUPS = "groups";

    // 好友表列名
    public static final String COLUMN_ID = "id";
    public static final String COLUMN_USERNAME = "username";
    public static final String COLUMN_NICKNAME = "nickname";
    public static final String COLUMN_AVATAR = "avatar";
    public static final String COLUMN_GROUP = "group_name";
    public static final String COLUMN_REMARK = "remark";
    public static final String COLUMN_STATUS = "status";

    // 分组表列名
    public static final String COLUMN_GROUP_NAME = "name";

    public FriendDatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // 创建好友表
        String createFriendsTable = "CREATE TABLE " + TABLE_FRIENDS + " (" +
                COLUMN_ID + " TEXT PRIMARY KEY," +
                COLUMN_USERNAME + " TEXT NOT NULL," +
                COLUMN_NICKNAME + " TEXT," +
                COLUMN_AVATAR + " TEXT," +
                COLUMN_GROUP + " TEXT," +
                COLUMN_REMARK + " TEXT," +
                COLUMN_STATUS + " TEXT" +
                ")";
        db.execSQL(createFriendsTable);

        // 创建分组表
        String createGroupsTable = "CREATE TABLE " + TABLE_GROUPS + " (" +
                COLUMN_GROUP_NAME + " TEXT PRIMARY KEY" +
                ")";
        db.execSQL(createGroupsTable);

        // 插入默认分组
        ContentValues values = new ContentValues();
        values.put(COLUMN_GROUP_NAME, "我的好友");
        db.insert(TABLE_GROUPS, null, values);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_FRIENDS);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_GROUPS);
        onCreate(db);
    }

    public long addFriend(Friend friend) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_ID, friend.getId());
        values.put(COLUMN_USERNAME, friend.getUsername());
        values.put(COLUMN_NICKNAME, friend.getNickname());
        values.put(COLUMN_AVATAR, friend.getAvatar());
        values.put(COLUMN_GROUP, friend.getGroup());
        values.put(COLUMN_REMARK, friend.getRemark());
        values.put(COLUMN_STATUS, friend.getStatus());
        return db.insert(TABLE_FRIENDS, null, values);
    }

    public List<Friend> getAllFriends() {
        List<Friend> friends = new ArrayList<>();
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query(TABLE_FRIENDS, null, null, null, null, null, null);

        if (cursor.moveToFirst()) {
            do {
                Friend friend = new Friend();
                friend.setId(cursor.getString(cursor.getColumnIndex(COLUMN_ID)));
                friend.setUsername(cursor.getString(cursor.getColumnIndex(COLUMN_USERNAME)));
                friend.setNickname(cursor.getString(cursor.getColumnIndex(COLUMN_NICKNAME)));
                friend.setAvatar(cursor.getString(cursor.getColumnIndex(COLUMN_AVATAR)));
                friend.setGroup(cursor.getString(cursor.getColumnIndex(COLUMN_GROUP)));
                friend.setRemark(cursor.getString(cursor.getColumnIndex(COLUMN_REMARK)));
                friend.setStatus(cursor.getString(cursor.getColumnIndex(COLUMN_STATUS)));
                friends.add(friend);
            } while (cursor.moveToNext());
        }
        cursor.close();
        return friends;
    }

    public void deleteFriend(String friendId) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_FRIENDS, COLUMN_ID + " = ?", new String[]{friendId});
    }

    public void updateFriend(Friend friend) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_USERNAME, friend.getUsername());
        values.put(COLUMN_NICKNAME, friend.getNickname());
        values.put(COLUMN_AVATAR, friend.getAvatar());
        values.put(COLUMN_GROUP, friend.getGroup());
        values.put(COLUMN_REMARK, friend.getRemark());
        values.put(COLUMN_STATUS, friend.getStatus());
        db.update(TABLE_FRIENDS, values, COLUMN_ID + " = ?", new String[]{friend.getId()});
    }

    public List<String> getAllGroups() {
        List<String> groups = new ArrayList<>();
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query(TABLE_GROUPS, new String[]{COLUMN_GROUP_NAME}, null, null, null, null, null);

        if (cursor.moveToFirst()) {
            do {
                groups.add(cursor.getString(0));
            } while (cursor.moveToNext());
        }
        cursor.close();
        return groups;
    }

    public void addGroup(String groupName) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_GROUP_NAME, groupName);
        db.insert(TABLE_GROUPS, null, values);
    }
} 